package cn.edu.zafu.bigdata.yt.yichangchuli;

public class ArrayIndexOutOfBoundsException_yt {
    public static int getElement(int[] arr, int index) {
        if (index < 0 || index >= arr.length) {
            throw new ArrayIndexOutOfBoundsException("Index " + index + " is out of bounds for array length " + arr.length);
        }
        return arr[index];
    }

    public static void main(String[] args) {
        int[] myArray = {1, 2, 3, 4, 5};  

        try {
            int element1 = getElement(myArray, 2);
            System.out.println("Element at index 2: " + element1);

            int element2 = getElement(myArray, 10);
            System.out.println("Element at index 10: " + element2);  
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
